
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import ${packageName}.model.${modelName};

/**
* @author：yzw
* @Date：${.now?string["yyyy/MM/dd"]}
* @desc：
*/

@Service
public class ${modelName}Service {

	@Resource
	private ${modelName}Mapper ${modelName?uncap_first}Mapper;

	public List<${modelName}> list(Map<String, Object> params){
		return ${modelName?uncap_first}Mapper.select(params);
	}

	public Long count(Map<String, Object> params){
		Long row = 0L;
		try{
			row = ${modelName?uncap_first}Mapper.count(params);
		}catch(Exception e){
			e.printStackTrace();
		}
		return row;
	}

	public boolean add(${modelName} ${modelName?uncap_first}){
		if(${modelName?uncap_first} == null) {
			return false;
		}
		return ${modelName?uncap_first}Mapper.insert(${modelName?uncap_first}) > 0;
	}

	public boolean update(${modelName} ${modelName?uncap_first}){
		if(${modelName?uncap_first} == null) {
			return false;
		}
		return ${modelName?uncap_first}Mapper.update(${modelName?uncap_first}) > 0;
	}

	public boolean remove(Map<String, Object> params){
		return ${modelName?uncap_first}Mapper.delete(params) > 0;
	}

	public int insertBatch(List<${modelName}> ${modelName?uncap_first}List) {
		int row = 0;
		if (CollectionUtils.isNotEmpty(${modelName?uncap_first}List)) {
			row = ${modelName?uncap_first}Mapper.insertBatch(${modelName?uncap_first}List);
		}
		return row;
	}

	public Message deleteBatch(List<String> ids) {
		if (CollectionUtils.isEmpty(ids)) {
			return new Message(Message.Status.FAIL);
		}
		int size = this.batchDelete(ids);
		return size > 0 ? new Message(Message.Status.SUCCESS) : new Message(Message.Status.FAIL);
	}

	public DataTablesResp<${modelName}> getDataList(DataTablesReqt dtparams) {
		int pageNum = dtparams.getLength() > 0 ? dtparams.getStart() / dtparams.getLength() + 1 : 0;
		int pageSize = dtparams.getLength();
		PageHelper.startPage(pageNum, pageSize, true);
		List<${modelName}> list = ${modelName?uncap_first}Mapper.getDataList(dtparams);
		PageInfo<${modelName}> pageInfo = new PageInfo<>(list);
		return new DataTablesResp<>(dtparams.getDraw(),
		pageInfo.getTotal(), pageInfo.getTotal(), pageInfo.getList(), "");
	}

	public Message save${modelName}(${modelName} ${modelName?uncap_first}) {
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		//判断是否存在重复名称
		Message message;
		boolean isSave;
		List<${modelName}> ${modelName?uncap_first}List = this.list(Collections.singletonMap("name", ${modelName?uncap_first}.getName()));
		if (CollectionUtils.isNotEmpty(${modelName?uncap_first}List)) {
			boolean isExist = StringUtils.isEmpty(${modelName?uncap_first}.getId())
			|| (StringUtils.isNotEmpty(${modelName?uncap_first}.getId()) && !${modelName?uncap_first}List.get(0).getId().equals(${modelName?uncap_first}.getId()));
			if (isExist) {
				message = new Message(Message.Status.FAIL);
				message.setDesc("名称【" + ${modelName?uncap_first}.getName() + "】已存在");
				return message;
			}
		}
		if (StringUtils.isEmpty(${modelName?uncap_first}.getId())) {
			${modelName?uncap_first}.setId(StringUtils.getUuid());
			${modelName?uncap_first}.setCreater(username);
			${modelName?uncap_first}.setCreatime(new Date());
			${modelName?uncap_first}.setOptlasttime(new Date());
			${modelName?uncap_first}.setOptlastname(username);
			isSave = this.add(${modelName?uncap_first});
		} else {
			${modelName?uncap_first}.setOptlastname(username);
			${modelName?uncap_first}.setOptlasttime(new Date());
			isSave = this.update(${modelName?uncap_first});
		}
		if (isSave) {
			message = new Message(Message.Status.SUCCESS);
			message.setDesc("保存成功");
		} else {
			message = new Message(Message.Status.FAIL);
			message.setDesc("保存失败");
		}
		return message;
	}

}